package 算法;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//https://leetcode-cn.com/problems/two-sum/
public class 两数之和 {
    public static void main(String[] args) {

    }
    //方法一，双重循环
    public static int[] twoSum1(int[] nums, int target) {
        //快速
        for (int i = 0; i < nums.length; i++) {
            for (int i1 = i+1; i1 < nums.length; i1++) {
                if(nums[i]+nums[i1] == target){
                    return new int[]{nums[i],nums[i1]};
                }
            }
        }
        return null;
    }
    //方法二，hashMap
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(hashMap.containsKey(target-nums[i])){
                //可以了，返回
                return new int[]{hashMap.get(target-nums[i]),i};
            }
           hashMap.put(nums[i],i);
        }
        return null;
    }
}
